import type {
  ComputeContext,
  CreateCrudOptionsRet,
} from '@fast-crud/fast-crud';

import { compute, dict } from '@fast-crud/fast-crud';

import { DefDatasourceConfigApi, DefTenantApi } from '#/api';
import { MultiTenantTypeEnum, TenantConnectTypeEnum } from '#/enums/system';
import { $t } from '#/locales';

const { VITE_GLOB_TENANT_TYPE } = import.meta.env;
export function createCrudOptions(): CreateCrudOptionsRet {
  return {
    crudOptions: {
      request: { editRequest: DefTenantApi.initDataRequest },
      form: { mode: 'edit', col: { span: 24 } },
      columns: {
        id: {
          title: $t('devOperation.tenant.defUser.username'),
          type: 'text',
          form: { show: false },
        },
        name: {
          title: $t('devOperation.tenant.defTenant.name'),
          type: 'text',
          form: { component: { disabled: true } },
        },
        connectType: {
          title: $t('devOperation.tenant.defTenant.connectType'),
          type: 'dict-radio',
          form: {
            value: TenantConnectTypeEnum.SYSTEM,
            rule: [{ required: true }],
          },
          dict: dict({
            data: [
              {
                label: '系统内置数据源',
                value: TenantConnectTypeEnum.SYSTEM,
              },
              {
                label: '自定义数据源',
                value: TenantConnectTypeEnum.CUSTOM,
                disabled:
                  MultiTenantTypeEnum.DATASOURCE_COLUMN !==
                  VITE_GLOB_TENANT_TYPE,
              },
            ],
          }),
        },
        baseDatasourceId: {
          title: '基础库',
          type: 'dict-select',
          form: {
            rule: [{ required: true }],
            show: compute((context: ComputeContext) => {
              return context.form?.connectType === TenantConnectTypeEnum.CUSTOM;
            }),
          },
          dict: dict({
            getData: DefDatasourceConfigApi.query,
            label: 'name',
            value: 'id',
          }),
        },
      },
    },
  };
}
